package com.yst.webplugin.config;

import com.yst.spring.context.SpringContext;
import com.yst.webplugin.component.CRUDInterceptor;
import com.yst.webplugin.component.FieldHandlerAdapter;
import com.yst.webplugin.component.FieldHandlerMapping;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
@Configuration
public class CRUDWebConfig implements WebMvcConfigurer {


    @Value("${spring.cors:false}")
    private boolean cors;

    private final CRUDInterceptor interceptor;
    private final List<HandlerInterceptor> interceptorList=new ArrayList<>();
    public CRUDWebConfig(){

        log.info("configure spring.cors=true will be allow cors!");

        interceptor=new CRUDInterceptor();
        SpringContext.addOnReadyListener(context -> {
            Map<String,HandlerInterceptor> interceptorMap=
                    SpringContext.getContext().getBeansOfType(HandlerInterceptor.class);
            interceptorList.addAll(interceptorMap.values());
        });
    }

    @Bean
    FieldHandlerMapping getFieldHandleMapping(){
        if (!interceptorList.contains(interceptor)){
            interceptorList.add(interceptor);
        }
        return new FieldHandlerMapping(interceptorList.toArray(new HandlerInterceptor[0]));
    }

    @Bean
    FieldHandlerAdapter getFieldHandlerAdapter(){
        return new FieldHandlerAdapter();
    }


    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        WebMvcConfigurer.super.configurePathMatch(configurer);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        WebMvcConfigurer.super.configureDefaultServletHandling(configurer);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor);
        WebMvcConfigurer.super.addInterceptors(registry);
    }

    @Bean
    @ConditionalOnProperty(prefix = "spring",value = "cors",havingValue = "true")
    public CorsFilter corsConfiguration(){
        log.warn("warning spring.cors=true");
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOriginPattern("*");
        config.setAllowCredentials(true);
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        config.addExposedHeader("*");
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(corsConfigurationSource);
    }
}
